|
|
|
|
|
|
|
Demonstrating the MsgBox Function |
|
|
|
|
|
|
|
|
To see the message box in action, create a simple program that utilizes it. You'll use a message box to confirm that a user who is attempting to exit the program is sure he or she wants to exit. Follow these steps: |
|
|
|
|
|
|
|
|
1. Start Visual Basic, if necessary, and begin a new Standard EXE project. |
|
|
|
|
|
|
|
|
2. Add a single command button to Form1. Set the command button's Name property to cmdExit and its Caption property to Exit. |
|
|
|
|
|
|
|
|
3. Enter the following code into cmdExit'sClick event procedure: |
|
|
|
 |
|
|
|
|
Dim sMsg As String
Dim nButtons As Integer
Dim nResult As Integer
sMsg = "Are you sure you want to exit?"
nButtons = vbYesNo + vbQuestion
nResult = MsgBox(sMsg, nButtons, "My Program")
If nResult = vbYes Then
End
End If |
|
|
|
|
|
|
|
|
4. Run the program and click the Exit button. You are asked whether you are sure you want to exit the program. Click the No button, and notice that the program does not close. |
|
|
|
|
|
|
|
|
Getting Information from the User |
|
|
|
|
|
|
|
|
Many times in a program, you need to get a single piece of information from the user. You might need the user to enter a name, the name of a file, or a number for various purposes. Although the message box lets your user make choices, it does not allow him or her to enter any information in response to the message. Therefore, you have to use some other means to get the information. Visual Basic provides a built-in dialog box for exactly this purpose: the input box. |
|
|
|
|
|
|
|
|
The input box displays a message to tell the user what to enter, a text box where the user can enter the requested information, and two command buttonsOK and Cancelthat can be used to either accept or abort the input data. A sample input box is shown in Figure 7.4. |
|
|
|
|
|
|
|
|
Figure 7.4
An input box lets the user enter a single
piece of data in response to a message. |
|
|
|
|
|
|
|
|
Setting Up the InputBox Function |
|
|
|
|
|
|
|
|
Programmatically, the input box works very much like the message box with a return value. You can specify a variable to receive the information returned from the input box and then |
|
|
|
|
|